<?php namespace sacvoyage\storage;


/*
 * erases keys obtained from keyIterator in table
 *
 */
class Eraser {

	public function __construct(&$table, &$keyIterator) {
		$this->_table=&$table;
		$this->_keyIterator=&$keyIterator;
	}

	public function go() {
		$count=0;
		// TODO: can be optimized to perform only one query
		foreach($this->_keyIterator as $key) {
			$_id=$this->_table->escape($key->_id);
			$sql="DELETE FROM {$this->_table->getExpression()} WHERE _id='$_id'";
			if(array_key_exists('rev', $key) && $key['rev']) {
				$rev=$this->_table->escape($key->_rev);
				$sql.=" AND _rev='$_rev'";
			}
			$this->_table->query($sql);
			$count++;
		}
		return $count;
	}


};
